package com.sjtu.vfact.textual.tablemodel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;

import javax.swing.table.AbstractTableModel;

import com.sjtu.vfact.textual.model.Pair;
import com.sjtu.vfact.textual.model.TermSet;
import com.sjtu.vfact.textual.util.TABLE_TYPE;

public class FACTATableModel extends AbstractTableModel{

	private static final long serialVersionUID = 339163904778384042L;
	
	private String[] columnNames;
	private List<Object[]> data;
	
	private TABLE_TYPE type;
	
	public FACTATableModel(TABLE_TYPE type){
		this.type = type;
		setColumnNames(type.getColumnNames());
		data = new ArrayList<Object[]>();
	}
	
	@Override
	public int getRowCount() {
		return data.size();
	}

	@Override
	public int getColumnCount() {
		return columnNames.length;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		return data.get(rowIndex)[columnIndex];
	}
	
	@Override
	public String getColumnName(int column) {
		return columnNames[column];
	}
	
	public void clear(){
		int rows = getRowCount();
		if(rows == 0){
			return;
		}
		
		data.clear();
		fireTableRowsDeleted(0, rows - 1);
		
	}

	private void setData(List<Object[]> data){
		this.data = data;
	}
	
	private void setColumnNames(String[] columnNames){
		this.columnNames = columnNames;
	}
	
	public void setTable(Map<TermSet, List<Pair<Integer, Integer>>> dict){
		List<Object[]> data = new ArrayList<Object[]>();
		
		int i = 1;
		for(Map.Entry<TermSet, List<Pair<Integer, Integer>>> entry : dict.entrySet()){
			Object[] tuple = new Object[getColumnCount()];
			TermSet termset = entry.getKey();
			List<Pair<Integer, Integer>> pairs = entry.getValue();
			
			tuple[0] = i;
			tuple[1] = termset.toString();
			tuple[2] = pairs.toString();
			
			data.add(tuple);
			i++;
		}
		
		setData(data);
	}
	
	public void setTable(List<Object[]> data){
		setData(data);
	}
	
}
